package com.qlslylq.ad.sdk.config;

import android.content.Context;
import android.text.TextUtils;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsInitCallback;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.SdkConfig;
import com.qlslylq.ad.sdk.application.AdMergeSDK;
import com.qlslylq.ad.sdk.application.AdMergeSDKApp;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.enums.ErrorEnum;
import com.qlslylq.ad.sdk.interfaces.InitCallback;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad.sdk.util.Log;

import static com.qlslylq.ad.sdk.model.rsp.MediaWebVO.NOT_CONFIG;


/**
 * 快手初始化控制类<br/>
 */
public class KsMgrHolder {

    private static boolean initSuccess = false;

    public static KsLoadManager get() {
        return KsAdSDK.getLoadManager();
    }

    public static void init(final Context context, final InitCallback callback) {
        doInit(context, callback);
    }

    /**
     * 初始化<br/>
     */
    private static void doInit(Context context, final InitCallback callback) {
        AdPlatformEnum platform = AdPlatformEnum.PLATFORM_KS;
        if (!initSuccess) {
            try {
                String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
                if (NOT_CONFIG.equals(appId)) {
                    Log.d(String.format("未配置%s账号到蒂烨平台", platform.getId()));
                    return;
                }
                if (TextUtils.isEmpty(appId)) {
                    Log.d(String.format("%s初始化失败：" + GsonUtils.gsonString(ErrorEnum.ERR_INIT_INVALID_ARG.rsp(platform, null)), platform.getId()));
                    if (callback != null) {
                        callback.fail(platform, ErrorEnum.ERR_INIT_INVALID_ARG.getId(), ErrorEnum.ERR_INIT_INVALID_ARG.getName());
                    }
                    return;
                }

                initSuccess = KsAdSDK.init(context, buildConfig(platform, callback));
                KsAdSDK.setPersonalRecommend(true);//设置是否启⽤个性化⼴告推荐
                KsAdSDK.setProgrammaticRecommend(true);//设置是否启⽤程序化⼴告推荐
            } catch (Exception e) {
                e.printStackTrace();
                Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + e.getMessage(), platform.getId()));
                if (callback != null) {
                    callback.fail(platform, ErrorEnum.ERR_INIT_THIRD_SDK.getId(), e.getMessage());
                }
            }
        }
    }

    private static SdkConfig buildConfig(AdPlatformEnum platform, final InitCallback callback) {
        String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
        return new SdkConfig.Builder()
                .appId(appId)
                .nightThemeStyleAssetsFileName("ks_adsdk_night_styles.xml")// Feed和入口组件，夜间模式样式配置，如果不配置 默认是"ks_adsdk_night_styles.xml"
                .showNotification(true) // 是否展示下载通知栏
                .debug(AdMergeSDKApp.isDebug()) // 是否开启sdk 调试⽇志 可选
                .setInitCallback(new KsInitCallback() {
                    public void onSuccess() {
                        initSuccess = true;
                        Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败"), platform.getId()));
                    }

                    public void onFail(int code, String msg) {
                        Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + code + "：" + msg, platform.getId()));
                        if (callback != null) {
                            callback.fail(platform, code, msg);
                        }
                    }
                })
                .build();
    }

    public static boolean isInitSuccess() {
        return initSuccess;
    }

    /**
     * 设置是否启用平台<br/>
     */
    public static void enablePlatform(boolean enable) {
        initSuccess = enable;
    }
}
